package com.example.lottoscratcho;

import java.util.Hashtable;
import java.util.Random;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;

/*
 * Match Card Alpha
 */

public class MatchCard extends Card {

	private final int NUM_SPOTS = 12;

	private final int TOP_LEFT_X = 80;
	private final int TOP_LEFT_Y = 500;
	private final int X_DIST = 185;
	private final int Y_DIST = 220;
	private final int NUM_COLUMNS = 3;
	private final int NUM_ROWS = 4;
	private final int TIMEBAR_X_POS = 600;
	private final int TIMEBAR_Y_POS = 500;
	
//---------------------------------------------------------------------------FIELDS:

	//spots (super)
	//scene (super)
	Hashtable <Integer, Integer> cardValueHash;
	boolean allLocked; //Do not allow any Spot to be revealed
	Spot firstSpotRevealed; //The spot that is currently revealed
	Spot secondSpotRevealed;
	Random rand;
	SceneManager manager;
	Scene currentScene;
	
//-----------------------------------------------------------------------CONSTRUCTOR:
	
	//if (matchCard.firstSpotRevealed == null) secondSpotRevealed = this;
	//else matchCard.firstSpotRevealed = this;
	
	/**
	 * Creates a new card
	 * @param card
	 */
	public MatchCard(Scene scene) {
		
		spots = new Spot[NUM_SPOTS];
		this.scene = scene;
		allLocked = false;
		firstSpotRevealed = null;
		secondSpotRevealed = null;
		rand = new Random();
		currentScene = scene;		
		manager = SceneManager.getInstance();
		
	}
	
//--------------------------------------------------------------------------METHODS:
		
	/**
	 * Creates all spots, generates values, attaches covers, and attaches all
	 * spots to the scene
	 * Iterates through the spots and attaches them all to currentScene
	 */
	public void start() {
		
		createSpots();
		generateValues();
		System.out.println("I'm making spots!");
		float x, y;
		int value;
		
		for (int i = 0; i < NUM_SPOTS; i++) {
			
			x = spots[i].xPos;
			y = spots[i].yPos;
			value = spots[i].value;
			makeBackdrop(x, y);
			//create a Text image and attach it to scene
			manager.makeMatchCardText(x, y, value);
			//create a SpotCover and attach it to scene
//			manager.makeMatchCardSpotCover(x, y, this);
					
		}
		
		manager.makeMatchCardTimeBar(TIMEBAR_X_POS, TIMEBAR_Y_POS);
		
		
	}
	
	/**
	 * Creates Spots, gives them locations  (but no SpotCovers or values)
	 * 
	 * Called before generateValues() and attachCovers()
	 */
	public void createSpots() {
		
		int xPos;
		int yPos;
		int i = 0; //Spot number
		
		for (int x = 0; x < NUM_COLUMNS; x++) {
			
			for (int y = 0; y < NUM_ROWS; y++) {
				
				//We mod by column number because we don't want to go off the screen
				xPos = TOP_LEFT_X + (x % NUM_COLUMNS) * X_DIST;
				yPos = TOP_LEFT_Y + (y % NUM_ROWS) * Y_DIST;
				
				spots[i] = new Spot(xPos, yPos, i);
				i++;
			}
		}
					
	}
	
	/**
	 * Iterates through the spots and randomly generates matching values
	 * 
	 * Called after createSpots() and before attachCovers()
	 */
	public void generateValues() {
		
		//fix capacity of hash table and initialize it
		int hashCapacity = NUM_SPOTS / 2;
		cardValueHash = new Hashtable<Integer, Integer>(hashCapacity);
		
		for (int i = 0; i < NUM_SPOTS; i++) {
			spots[i].value = -1;
		}
		
		//iterate over each square
		for (int i = 0; i < NUM_SPOTS; i++) {
			//if it has not been assigned a value: (values initialized to -1)
			if (spots[i].value == -1) { 
				
				int randVal = rand.nextInt(100);
				
				//if this value has been used, pick another
				while (cardValueHash.contains(randVal) == true) {
					randVal = rand.nextInt(100);
				}
				
				//set spot's value
				spots[i].value = randVal;
				
				//pick a random spot for matching value
				Spot randSpot = spots[rand.nextInt(NUM_SPOTS)];
				
				//if it already has a value, pick another
				while (randSpot.value != -1) {
					randSpot = spots[rand.nextInt(NUM_SPOTS)];
				}
				
				//assign the other spot the same value
				randSpot.value = randVal;
				
				//put new value in hash table
				cardValueHash.put(randVal, 2);
			}

		}
		
	}

	
	
	//---------TO FARM:
	public void makeBackdrop(float xPos, float yPos) {

		ITextureRegion backdrop = manager.getBackdrop();
			
		Sprite newSprite = new Sprite(xPos, yPos, 
									  backdrop, 
									  manager.getEngine().getVertexBufferObjectManager());
		currentScene.attachChild(newSprite);
	//---------
	}
	
}
